package com.kuang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.pojo.Product;
import com.kuang.pojo.Stock;
import com.kuang.pojo.supplier;
import com.kuang.service.ProductServiceImpl;
import com.kuang.service.StockServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.kuang.service.supplierServiceImpl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
//进货模块
@Controller
@RequestMapping("/stock")
public class StockController {
    @Autowired
    @Qualifier("stockservice")
    private StockServiceImpl stockService;
    @Autowired
    private supplierServiceImpl supplierService;
    @Autowired
    @Qualifier("productservice")
    private ProductServiceImpl productService;
//显示全部的进货信息
    @RequestMapping("/getStock")
    public String  getStock(@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum, @RequestParam(required=true,value="pageSize",defaultValue="6")Integer pageSize, Model model){
        PageHelper.startPage(pageNum,pageSize);
        List<Stock> stock = stockService.getStock();
        PageInfo<Stock> stockPageInfo=new PageInfo<>(stock);
        if (pageNum==0 ||pageNum>stockPageInfo.getPages()) {
            return "redirect:/stock/getStock";
        }
        model.addAttribute("stockpageInfo",stockPageInfo);
        return "stock";
    }
//    @RequestMapping("/getStock02")
//    public String  getStock02(@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum, @RequestParam(required=true,value="pageSize",defaultValue="6")Integer pageSize, Model model){
//        PageHelper.startPage(pageNum,pageSize);
//        List<Stock> stock = stockService.getStock();
//        PageInfo<Stock> stockPageInfo=new PageInfo<>(stock);
//        if (pageNum==0 ||pageNum>stockPageInfo.getPages()) {
//            return "redirect:/stock/getStock02";
//        }
//        model.addAttribute("stockpageInfo",stockPageInfo);
//        return "stockbyName";
//
//
//
//    }
    //转到增加界面====================================
 //跳转到增加信息页面
    @RequestMapping("/redirctadd")
    public String redirectadd(Model model){
        List<supplier> supplier = supplierService.getSupplier();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
        simpleDateFormat.format(date);
        model.addAttribute("supplier",supplier);
        model.addAttribute("date",date);
        return "addstock";
    }
//    //转到增加界面
//    @RequestMapping("/redirctadd02")
//    public String redirectadd02(Model model){
//        Date date = new Date();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
//        simpleDateFormat.format(date);
//        model.addAttribute("date",date);
//        return "/putong/addstock";
//    }
    //增加订单  进货模块增加订单 =============================增加订单=========================
    //增加信息页面
    @RequestMapping("/addstock")
    public String addStock(Stock stock,String saleprice){
        //新增一条进货信息
        String price = stock.getPrice();//价格
        int number1 = stock.getNumber();//新输入的数量
        //如果没有该商品 在库存中新增一条
        String size = Integer.toString(number1);
        String sume = stock.getSume();//供货商
        String detail = stock.getDetail();
        Date date = new Date();
        stock.setDate(date);

        stockService.addStock(stock);//新增一条数据

        System.out.println("1");


        String name = stock.getName();
        List<Product> product = productService.getProduct();
        //如果进货的商品  在库存表中有  就改变数量
        for (Product value : product) {
            //如果名称有相同的
            if ((value.getName().equals(name))) {
                //如果供应商
                if (value.getSuppiler().equals(sume)) {
                  // 如果价格相同
                if (value.getJinjia().equals(price)){
                    String number = productService.productbyname(name);//获取库存表的数量
                    //进行类型转换
                    int i = Integer.parseInt(number);
                    int sum=i+number1;
                    String s = Integer.toString(sum);
                    int productidbyname = productService.productidbyname(name);
                    productService.updateById(new Product(productidbyname,s));
                    return "redirect:/stock/getStock";
                }else {
                    //
                    //  供货商相同，但是进价钱不同
                String name1=price+"元的"+name;
                productService.addProduct(new Product(name1,saleprice,size,sume,price,detail));
                return "redirect:/stock/getStock";
                }
                }else{
                    //如果供货商不同
                    String name1=sume+"的"+name;
                    productService.addProduct(new Product(name1,saleprice,size,sume,price,detail));
                    return "redirect:/stock/getStock";

                }
            }

        }

        productService.addProduct(new Product(name,saleprice,size,sume,price,detail));


        return "redirect:/stock/getStock";
    }
    //增加订单
//    @RequestMapping("/addstock02")
//    public String addStock02(Stock stock){
//        Date date = new Date();
//        stock.setDate(date);
//        stockService.addStock(stock);
//        System.out.println("1");
//        String id = stock.getId();
//        String name = stock.getName();
//        List<Product> product = productService.getProduct();
//        for (int i=0;i<product.size();i++){
//            if (product.get(i).getId().(id)){
//                productService.updateById(new Product(product.get(i).getId(),product.get(i).getNumber()+stock.getNumber()));
//
//            }
//
//        }
//        return "redirect:/stock/getStock02";
//    }
 //跳转到修改信息页面
    @RequestMapping("/redirectupdate")
    public String redirectUpdate(String id,Model model){
        Stock stock = stockService.getStockById(id);
        model.addAttribute("stock",stock);
        return "updatastock";
    }
//    @RequestMapping("/redirectupdate02")
//    public String redirectUpdate02(String id,Model model){
//
//        Stock stock = stockService.getStockById(id);
//        model.addAttribute("stock",stock);
//        return "/putong/updatastock";
//    }
    //  ===========================修改进货订单=====================
//修改进货信息
    @RequestMapping("/updatestock")
    public String update(Stock stock){
        //获取商品的名字  进价  数量  供应商 电话 备注
        String name = stock.getName();//名称
        String price = stock.getPrice();//进价
        int number = stock.getNumber();//数量
        String s = Integer.toString(number);
        String sume = stock.getSume();//获取供应商
        String tellpone = stock.getTellpone();//获取电话
        String detail = stock.getDetail();//备注
        System.out.println(
                "========="+detail+"==========="
        );
        //根据名字获取库存表的 进价  数量
        int productid= productService.productidbyname(name);//根据名称获取id
//        Product product = productService.getProductById(productid);//根据id查询商品信息
//        String jinjia = product.getJinjia();//获取商品的进价
//        String productnumber = product.getNumber();//获取商品库存的数量
//        //数量进行类型转换
//        int productnumber02 = Integer.parseInt(productnumber);
       //根据商品名称修改  进价  数量  供应商  电话
        System.out.println(productid);
        System.out.println(s);
        System.out.println(sume);
        System.out.println(price);
        productService.updatensjbyid(new Product(productid,s,sume,price));
        stockService.updateStock(stock);
        return "redirect:/stock/getStock";
    }
//    @RequestMapping("/updatestock02")
//    public String update02(Stock stock){
//        stockService.updateStock(stock);
//        return "redirect:/stock/getStock02";
//    }
//删除订单
//删除信息
    @RequestMapping("/deletestock")
    public String delete(String id){
        stockService.deleteStock(id);
        return "redirect:/stock/getStock";
    }
//    //删除订单
//    @RequestMapping("/deletestock02")
//    public String delete02(String id){
//        stockService.deleteStock(id);
//        return "redirect:/stock/getStock02";
//    }
    //根据商品编号查询订单
//根据id查询信息
    @RequestMapping("/getStockById")
public String getStockById(String id,Model model){
    Stock stockById = stockService.getStockById(id);
    model.addAttribute("product",stockById);
    return "getstockbyid";
}
//    //根据商品编号查询订单
//    @RequestMapping("/getStockById02")
//    public String getStockById02(String id,Model model){
//        Stock stockById = stockService.getStockById(id);
//        model.addAttribute("product",stockById);
//        return "/putong/getstockbyid";
//    }
//根据名称模糊查询商品信息
//根据名称模糊查询进货洗洗
    @RequestMapping("/getStockByName")
    public String getStockByName(Model model,String name){
        List<Stock> stockByName = stockService.getStockByName(name);
        model.addAttribute("stockByName",stockByName);
        return "stockbyName";
    }
}
